﻿; !function () {
    var kwindow = {
        v: "2.2",
        ie6: !!window.ActiveXObject && !window.XMLHttpRequest,
        index: 100000
    };
    kwindow.enums = {
        type: { alert: 0,confirm:6,msg: 1, prompt:2,loading: 4,win:5},
        contType: { iframe: 100, dom: 101, ajax: 102 },
        promptType:{text:0,textarea:1},
        anim: { def: 'kui-anim', anim_01: 'kui-anim-01', anim_02: 'kui-anim-02', anim_03: 'kui-anim-03', anim_04: 'kui-anim-04', anim_05: 'kui-anim-05', anim_06: 'kui-anim-06' },
        classes: { kwindow: 'kui-window', title: 'kui-window-title', dialog: 'kui-window-dialog', iframe: 'kui-window-iframe', content: 'kui-window-content', buttons: 'kui-window-btns', options: 'kui-window-options', close: 'kui-window-close', min: 'kui-window-min', max: 'kui-window-max', normal: 'kui-window-normal', loadingText: 'kui-window-loading-text' }
    };

    //easing动画:http://jqueryui.com/easing/
    kwindow.config = {//全局默认配置
        //shade: { opacity: 0.3, bgColor: '#000' },
        shadeStyle:{background_color:'#000',opacity:'0.3'},
        anim: kwindow.enums.anim.def,
        time: 0,
        title: '信息',
        move: '.kui-window-title', //触发拖动的元素.
        showTitleBar: true,
        //closeAnim: { name: 'linear', params: { width: 0, height: 0, opacity: 0 }, speed: 200 },//easeInQuad
        closeAnim:{className:'kui-anim-close',speed:200},//speed的值设置为与classsName对应的样式一致,单位为毫秒.
        minimizeAnim: { name: 'linear', params: {}, speed: 200 },
        normalizeAnim: { name: 'linear', params: {}, speed: 200 },
        maximizeAnim: { name: 'linear', params: {}, speed: 200 }
    };
    var obj = function (config) {
        kwindow.index += 2;//遮罩层index=kwindow.index-1
        this.index = kwindow.index;
        this.config = config;
        if (this.config.max!=false&&!this.config.max) {
            this.config.max = {}
        }
        if (this.config.shadeStyle) {
            this.config.shadeStyle.filter = 'alpha(opacity=' + (parseFloat(this.config.shadeStyle.opacity) * 100) + ')';
        }

        kwindow['obj_' + kwindow.index] = this;

        this.tempData = {};//方便存放一些临时数据.

        this.create();

        this.args = this.config.args || {};//交互操作,比如iframe子父页面交互操作传递参数都通过args,args从父页面传到子页面,又从子页面传到父页面.
        this.args.index=this.index;
        this.args.kwindow = $('#kui-window-' + this.index + '');
        this.args.close=true;//回调函数中通过设置close的值为false可以阻止弹出框关闭.

        if (this.config.onShow) {
            this.config.onShow(this.args);//以便让用户使用js进行各种操作,例如用js对外观进行修改等等.
        }
    }
    obj.prototype.createShade = function () {//创建遮罩层
        //alert(this.config.shade.opacity);
        if (this.config.shade == false) {
            return;
        }
        var html = '<div id="kui-window-shade-' + this.index + '" class="kui-window-shade" style="z-index:' + (this.index - 1) + ';" index="' + this.index + '"></div>';
        $('body').append(html);
    };
    obj.prototype.create = function () {
        var _obj = this;
        this.createShade();
        var _className = '';
        switch (this.config.type) {
            case kwindow.enums.type.alert:
                _className = 'kui-window-alert';
                break;
            case kwindow.enums.type.confirm:
                _className = 'kui-window-confirm';
                break;
            case kwindow.enums.type.msg:
                _className = 'kui-window-msg';
                break;
            case kwindow.enums.type.prompt:
                _className = 'kui-window-prompt';
                break;
            case kwindow.enums.type.loading:
                _className = 'kui-window-loading';
                break;
            case kwindow.enums.type.win:
                _className = 'kui-window-win';
                break;
            default:
                break;
        }
        
        if ((('string' == typeof _obj.config.content) && _obj.config.content.substr(0, 4) == 'url:')
            || (('string' == typeof this.config.content) && this.config.content.substr(0, 9) == 'ajax-get:')
            || (('string' == typeof this.config.content) && this.config.content.substr(0, 10) == 'ajax-post:')
            )
        {
            if (_obj.config.showLoading == undefined) {
                _obj.config.showLoading = true;
            }
            if (_obj.config.showLoadingDelay == undefined) {
                _obj.config.showLoadingDelay = 1000;
            }
            if (_obj.config.showLoadingWindow == undefined) {
                _obj.config.showLoadingWindow = window.kwindow;//top.kwindow
            }
        }

        var contentHtml = '';
        //if ('string' == typeof this.config.content) {
        //    contentHtml = this.config.content;
        //}
        if (('string' == typeof _obj.config.content) && _obj.config.content.substr(0, 4) == 'url:') {
            //scrolling:'yes'、'no'、'auto'
            var guid = Guid.NewGuid();
            top[guid]={loadingIndex:0};
            top[guid].isLoaded = false;
            top[guid].kwindow = _obj.config.showLoadingWindow;
            if (_obj.config.showLoading == true && _obj.config.showLoadingWindow) {
                if (_obj.config.showLoadingDelay > 0) {
                    var _timeoutID = window.setTimeout(function () {
                        window.clearTimeout(_timeoutID);
                        if (!top[guid].isLoaded) {
                            top[guid].loadingIndex = _obj.config.showLoadingWindow.loading(_obj.config.loadingConfig);
                        }
                    }, _obj.config.showLoadingDelay)
                }
                else {
                    top[guid].loadingIndex = _obj.config.showLoadingWindow.loading(_obj.config.loadingConfig);
                }
            }
            //alert('a'+loadingIndex);
            contentHtml = '<iframe scrolling="' + (_obj.config.scrolling || "auto") + '" allowtransparency="true" id="kui-window-iframe-' + _obj.index + '" name="kui-window-iframe-' + _obj.index + '" class="kui-window-iframe" frameborder="0" onload="javascript:top[\'' + guid + '\'].isLoaded=true;top[\'' + guid + '\'].kwindow.close(top[\'' + guid + '\'].loadingIndex);" src="' + _obj.config.content.substr(4) + '"></iframe>'
            this.config.contentPartStyle=$.extendEx({}, { overflow: 'hidden' }, this.config.contentPartStyle);//如果载加的iframe页面有水平滚动条,滚动条本身有一定高度,导致会出现垂直滚动条,理论上如果有水平滚动条contentHeight还应减去水平滚动本身本高度.由于滚动条本身高度水大,这里通过overflow:hidden强制不显示滚动条也是可以的.
        }
        else if (('string' == typeof this.config.content) && this.config.content.substr(0, 9) == 'ajax-get:') {
            contentHtml = '';
        }
        else if (('string' == typeof this.config.content) && this.config.content.substr(0, 10) == 'ajax-post:') {
            contentHtml = '';
        }
        else {
            contentHtml = this.config.content;
            if (!contentHtml) {
                contentHtml = '';
            }
        }

        var html = '<div id="kui-window-' + this.index + '" class="kui-window ' + _className + '" style="z-index:' + this.index + '" kwindow-index="' + this.index + '" kwindow-type="' + this.config.type + '">';
        if (this.config.showTitleBar) {
            html += '<div class="' + kwindow.enums.classes.title + '">' + this.config.title + '</div>';
        }

        //content
        html += '<div class="kui-window-content">';
        html += contentHtml;
        html += '</div>';

        if (this.config.min != false || this.config.max != false || this.config.close != false) {
            html += '<div class="' + kwindow.enums.classes.options + '">';
            if (this.config.min != false) {
                html += '<a class="' + kwindow.enums.classes.min + '"><icon></icon></a>';
            }
            if (this.config.max != false) {
                html += '<a class="' + kwindow.enums.classes.max + '"></a>';
            }
            if (this.config.min !=false || this.config.max != false) {
                html += '<a class="' + kwindow.enums.classes.normal + '" style="display:none;"></a>';
            }
            if (this.config.close != false) {
                html += '<a class="' + kwindow.enums.classes.close + '"></a>';
            }
            html += '</div>';
        }

        //按钮栏容器
        if (this.config.buttons) {
            html += '<div class="' + kwindow.enums.classes.buttons + ' ' + kwindow.enums.classes.buttons + '-' + this.config.buttons.length + '"></div>';
        }
        html += '</div>';

        $('body').append(html);

        var jo = $('#kui-window-' + _obj.index + '');
        if (_obj.config.anim) {
            jo.addClass(_obj.config.anim);
        }

        var shadePart = $('#kui-window-shade-' + _obj.index + '');
        var titlePart = $('#kui-window-' + _obj.index + ' .' + kwindow.enums.classes.title + '');
        var optionsPart = $('#kui-window-' + _obj.index + ' .' + kwindow.enums.classes.options + '');
        var contentPart = $('#kui-window-' + _obj.index + ' .' + kwindow.enums.classes.content + '');
        var loadingPart = $('#kui-window-' + _obj.index + ' .' + kwindow.enums.classes.content + '>div');
        var loadingTextPart = $('#kui-window-' + _obj.index + ' .' + kwindow.enums.classes.loadingText + '');
        var buttonsPart = $('#kui-window-' + _obj.index + ' .' + kwindow.enums.classes.buttons + '');//最下一栏按钮栏
        var closeButton = $('#kui-window-' + _obj.index + ' .' + kwindow.enums.classes.close + '');//右上角关闭图标
        var minButton = $('#kui-window-' + _obj.index + ' .' + kwindow.enums.classes.min + '');//右上角最小化图标
        var maxButton = $('#kui-window-' + _obj.index + ' .' + kwindow.enums.classes.max + '');//右上角最大化图标
        var normalButton = $('#kui-window-' + _obj.index + ' .' + kwindow.enums.classes.normal + '');//右上角窗口还原到正常图标
        var iframePart = $('#kui-window-' + _obj.index + ' iframe');
      

        var loadingIndex = 0;
        var isLoaded = false;
        if ((('string' == typeof _obj.config.content) && _obj.config.content.substr(0, 9) == 'ajax-get:')
            || (('string' == typeof _obj.config.content) && _obj.config.content.substr(0, 10) == 'ajax-post:')
            )
        {
            if (_obj.config.showLoading == true && _obj.config.showLoadingWindow) {
                if (_obj.config.showLoadingDelay > 0) {
                    var _timeoutID = window.setTimeout(function () {
                        window.clearTimeout(_timeoutID);
                        if (!isLoaded) {
                            loadingIndex = _obj.config.showLoadingWindow.loading(_obj.config.loadingConfig);
                        }
                    }, _obj.config.showLoadingDelay)
                }
                else {
                    loadingIndex = _obj.config.showLoadingWindow.loading(_obj.config.loadingConfig);
                }
            }
        }

        if (('string' == typeof _obj.config.content) && _obj.config.content.substr(0, 9) == 'ajax-get:') {
            var url = _obj.config.content.substr(9);
            $.get(url, function (data, status) {
                isLoaded = true;
                kwindow.close(loadingIndex);
                contentPart.html(data);
            });
        }
        else if (('string' == typeof _obj.config.content) && _obj.config.content.substr(0, 10) == 'ajax-post:') {
            var url = this.config.content.substr(10);
            $.post(url, {}, function (data, status) {
                isLoaded = true;
                kwindow.close(loadingIndex);
                contentPart.html(data);
            });
        }

        //按钮栏放置按钮.
        if (!this.config.buttons) {
            this.config.buttons = [];
        }
        $.each(this.config.buttons, function (i, button) {
            var _btn = $('<a class="kui-window-btn-' + i + '">' + button.text + '</a>').appendTo(buttonsPart);
            _btn.setStyle(button.style);
            $(_btn).click(function () {
                //var arg = undefined;
                if (_obj.config.type == kwindow.enums.type.prompt) {
                    var joText = $('input', jo);
                    if (joText.length > 0) {
                        //arg = joText.val();
                        _obj.args.promptResult = joText.val();
                    }
                    else {
                        //arg = $('textarea', jo).val();
                        _obj.args.promptResult = $('textarea', jo).val();
                    }
                }
                _obj.args.closeTrigger = 'btn-' + i + '';
                if (button.close == 'first') {//关闭后再执行回调.
                    if (button.close != false) {//不关闭对话框
                        kwindow.close(_obj.index);
                    }
                    if (button.onClick) {
                        
                        button.onClick(_obj.args);
                    }
                }
                else {//执行回调后再关闭对话框.
                    if (button.onClick) {
                        button.onClick(_obj.args);
                    }
                    if (button.close != false) {//不关闭对话框
                        kwindow.close(_obj.index);
                    }
                }
            });
        });

        //以上完成所有元素添加到页面后才进行宽高计算,这样才是正确的.
        jo.setStyle(this.config.style);
        titlePart.setStyle(this.config.titlePartStyle);
        buttonsPart.setStyle(this.config.buttonsPartStyle);
        //autoContentHeight()用到jo.height、titlePart.height、buttonsPart.height,所以上面三句放在前面.
        this.autoContentHeight();//放置setStyle()方法前执行,以便用户可以自定义高度覆盖.

        if (this.config.type == kwindow.enums.type.loading) {
            if (this.config.loadingText) {
                $(loadingPart).css('width', 'auto');//放置setStyle()方法前执行,以便用户可以自定义宽度覆盖.
            }
        }

        shadePart.setStyle(this.config.shadeStyle);
        
        optionsPart.setStyle(this.config.optionsPartStyle);
        
        //alert(this.config.contentPartStyle+'aa');
        contentPart.setStyle(this.config.contentPartStyle);
        loadingPart.setStyle(this.config.loadingPartStyle);
        loadingTextPart.setStyle(this.config.loadingTextPartStyle);
        
        closeButton.setStyle(this.config.closeButtonStyle);
        minButton.setStyle(this.config.minButtonStyle);
        maxButton.setStyle(this.config.maxButtonStyle);
        normalButton.setStyle(this.config.normalButtonStyle);
        iframePart.setStyle(this.config.iframePartStyle);
        //关闭
        closeButton.click(function () {//点击右上角关闭图标关闭
            _obj.args.closeTrigger = 'closeButton';
            kwindow.close(_obj.index);
        });
        
        //最小化
        minButton.click(function () {
            _obj.config.normal = {
                //left: $(jo).position().left,
                //top: $(jo).position().top,
                //left: $(jo).css('left'),//config中没有定义left和top时,是居中对齐,left和top与窗口大小有关,无法确定left和top的值.
                //top:$(jo).css('top'),
                width: $(jo).width(),
                height: $(jo).height()
            };
            
            if (_obj.config.minimizeAnim != false) {
                jo.animate($.extendEx({
                    height: titlePart.height(),
                    //opacity: 0
                },_obj.config.minimizeAnim), _obj.config.minimizeAnim.speed, _obj.config.minimizeAnim.name, function () {
                    contentPart.hide();
                    minButton.hide();
                    buttonsPart.hide();
                    maxButton.hide();
                    normalButton.show();
                    jo.css('height', 'auto');
                });
            }
            else {
                contentPart.hide();
                minButton.hide();
                buttonsPart.hide();
                maxButton.hide();
                normalButton.show();
                jo.css('height', 'auto');
            }
            _obj.config.sizeStatus = 'min';
            if (_obj.config.onMin) {
                _obj.config.onMin();
            }
            
        });
        //最大化
        maxButton.click(function () {
            _obj.config.normal = {//config中没有定义left和top时,是居中对齐,left和top与窗口大小有关,无法确定left和top的值.
                width: $(jo).width(),
                height: $(jo).height()
            };
            contentPart.show();
            minButton.hide();
            maxButton.hide();
            normalButton.show();


            var max_width = _obj.config.max.width;
            var max_height = _obj.config.max.height;
            var max_left = _obj.config.max.left;
            var max_top = _obj.config.max.top;
            if (!max_width) {
                max_width = $(window).width();
            }
            if (!max_height) {
                max_height = $(window).height();
            }
            if (!max_left) {
                max_left = 0;
            }
            if (!max_top) {
                max_top = 0;
            }
            
            //最大化之前先将左上角坐标保存起来,以便正常化时动画使用,否则动画不知道从哪里变到哪里.
            //注:这个左上角坐标是临时的.因为居中对齐时坐标是无法确定的,因为浏览器窗口大小会变化.
            _obj.tempData.normal = {
                left: jo.position().left,
                top: jo.position().top,
            };

            if (_obj.config.maximizeAnim != false) {
                buttonsPart.hide();//动画过程中,由于contentPart的高度不变,buttonsPart位置不变,这样看起来不好看,所以先隐藏好了.
                jo.animate($.extendEx({
                    width: max_width,
                    height: max_height,
                    left: max_left,
                    top: max_top,
                    //opacity: 1
                },_obj.config.maximizeAnim), _obj.config.maximizeAnim.speed, _obj.config.maximizeAnim.name, function () {
                    buttonsPart.show();
                    jo.css('left', max_left);
                    jo.css('top', max_top);
                    jo.css('width', max_width);
                    jo.css('height', max_height);
                    _obj.autoContentHeight();
                });
            }
            else {
                jo.css('left', max_left);
                jo.css('top', max_top);
                jo.css('width', max_width);
                jo.css('height', max_height);
                _obj.autoContentHeight();
            }
            $(window).resize(function () {
                if (_obj.config.sizeStatus == 'max') {
                    if (!_obj.config.max.left) {
                        jo.css('left', 0);
                    }
                    if (!_obj.config.max.top) {
                        jo.css('top', 0);
                    }
                    if (!_obj.config.max.width) {
                        jo.css('width', $(window).width());
                    }
                    if (!_obj.config.max.height) {
                        jo.css('height', $(window).height());
                        _obj.autoContentHeight();
                    }
                }
            });
            _obj.config.sizeStatus = 'max';
            if (_obj.config.onMax) {
                _obj.config.onMax();
            }
        });
        normalButton.click(function () {
            if (_obj.config.sizeStatus == 'min') {
                
                if (_obj.config.normalizeAnim != false) {
                    jo.animate($.extendEx({
                        width: _obj.config.normal.width,
                        height: _obj.config.normal.height,
                        //opacity: 0
                    },_obj.config.normalizeAnim), _obj.config.minimizeAnim.speed, _obj.config.minimizeAnim.name, function () {
                        contentPart.show();
                        minButton.show();
                        buttonsPart.show();
                        maxButton.show();
                        normalButton.hide();
                        //先设置width和height，再调用setPosition方法,计算居中对齐时要用到width和height.
                        $(jo).css('width', _obj.config.normal.width);
                        $(jo).css('height', _obj.config.normal.height);
                        _obj.setPosition();
                    });
                }
                else {
                    contentPart.show();
                    minButton.show();
                    buttonsPart.show();
                    maxButton.show();
                    normalButton.hide();
                    //先设置width和height，再调用setPosition方法,计算居中对齐时要用到width和height.
                    $(jo).css('width', _obj.config.normal.width);
                    $(jo).css('height', _obj.config.normal.height);
                    _obj.setPosition();
                }
            }
            if (_obj.config.sizeStatus == 'max') {
                

                if (_obj.config.normalizeAnim != false) {
                    jo.animate($.extendEx({
                        width: _obj.config.normal.width,
                        height: _obj.config.normal.height,
                        left: _obj.tempData.normal.left,
                        top: _obj.tempData.normal.top,
                        //opacity: 0
                    }, _obj.config.normalizeAnim), _obj.config.minimizeAnim.speed, _obj.config.minimizeAnim.name, function () {
                        minButton.show();
                        maxButton.show();
                        normalButton.hide();
                        //先设置width和height，再调用setPosition方法,计算居中对齐时要用到width和height.
                        $(jo).css('width', _obj.config.normal.width);
                        $(jo).css('height', _obj.config.normal.height);
                        _obj.setPosition();
                        _obj.autoContentHeight();
                    });
                }
                else {
                    minButton.show();
                    maxButton.show();
                    normalButton.hide();
                    //先设置width和height，再调用setPosition方法,计算居中对齐时要用到width和height.
                    $(jo).css('width', _obj.config.normal.width);
                    $(jo).css('height', _obj.config.normal.height);
                    _obj.setPosition();
                    _obj.autoContentHeight();
                }
            }
            _obj.config.sizeStatus = 'normal';
            if (_obj.config.onNormal) {
                _obj.config.onNormal();
            }
        });
        this.setPosition();
        this.move();
        //定时关闭
        if (this.config.time && this.config.time > 0) {
            var timerId = setTimeout(function () {
                clearTimeout(timerId);
                _obj.args.closeTrigger = 'timer';//定时关闭.
                kwindow.close(_obj.index);
            }, this.config.time);
        }
    };
    //obj.prototype.setStyle=function(selector,style){
    //    if (style&&('object'==typeof style)&& $(selector).length > 0) {
    //        //var styles = styleStr.split(';');
    //        $.each(style, function (k, v) {
    //            k = k.replace(/_/g, "-");//js变量名不能有'-',css中样式名都是'-'.注意:k.replace('_', '-')只会替换第一个'_'.
    //            v = $.trim(v);
    //            if (v == '') {
    //                return;
    //            }
    //            $(selector).css(k, v);
    //        });
    //    }
    //};
    obj.prototype.autoContentHeight = function () {
        var jo = $('#kui-window-' + this.index + '');
        var titlePart = $('#kui-window-' + this.index + ' .' + kwindow.enums.classes.title + '');
        var contentPart = $('#kui-window-' + this.index + ' .' + kwindow.enums.classes.content + '');
        var buttonsPart = $('#kui-window-' + this.index + ' .' + kwindow.enums.classes.buttons + '');//最下一栏按钮栏
        

        //outerHeight(true):包括:padding、margin和border.参数不传true时不包括margin.
        //innerHeight():包括padding
        //height()://不包括adding、margin和border.

        var contentHeight =parseInt($(jo).height(),10) - (titlePart.length > 0 ? parseInt(titlePart.outerHeight(true),10) : 0) - (buttonsPart.length > 0 ? parseInt(buttonsPart.outerHeight(true),10) : 0) - parseInt(contentPart.css('margin-top').split('px')[0], 10) - parseInt(contentPart.css('margin-bottom').split('px')[0], 10) - parseInt(contentPart.css('padding-top').split('px')[0], 10) - parseInt(contentPart.css('padding-bottom').split('px')[0], 10) - parseInt(contentPart.css('border-top-width').split('px')[0], 10) - parseInt(contentPart.css('border-bottom-width').split('px')[0], 10);
        contentPart.css('height', contentHeight);//放置setStyle()方法前执行,以便用户可以自定义高度覆盖.
        
    };
    
    obj.prototype.setPosition = function () {//设置left和top
        var _obj = this;

        var _kwindow = $('#kui-window-' + this.index + '');
        
        var _left = null;
        if (this.config.style) {
            _left = this.config.style.left;
        }
        
        var _top = null;
        if (this.config.style) {
            _top=this.config.style.top;
        }
        if (_left == 'r') {//水平靠右
            _left = $(window).width() - $(_kwindow).outerWidth();
            _left += (this.config.leftAdd ? this.config.leftAdd : 0);
            _kwindow.css('left', _left);
            $(window).resize(function () {
                _left = $(window).width() - $(_kwindow).outerWidth();
                _left += (_obj.config.leftAdd ? _obj.config.leftAdd : 0);
                _kwindow.css('left', _left);
            });
        }
        else if (_left==0||_left) {
            _kwindow.css('left', _left);
        }
        else {//水平居中
            //没有定义left则默认水平居中.
            _left = ($(window).width() - $(_kwindow).outerWidth()) * 0.5;//outerWidth()
            _left += (this.config.leftAdd ? this.config.leftAdd : 0);
            _kwindow.css('left', _left);
            $(window).resize(function () {
                _left = ($(window).width() - $(_kwindow).outerWidth()) * 0.5;
                _left += (_obj.config.leftAdd ? _obj.config.leftAdd : 0);
                _kwindow.css('left', _left);
            });
        }
        if (_top == 'b') {
            _top = $(window).height() - $(_kwindow).outerHeight();
            _top += (this.config.topAdd ? this.config.topAdd : 0);
            _kwindow.css('top', _top);
            $(window).resize(function () {
                _top = $(window).height() - $(_kwindow).outerHeight();
                _top += (_obj.config.topAdd ? _obj.config.topAdd : 0);
                _kwindow.css('top', _top);
            });
        }
        else if (_top==0||_top) {
            _kwindow.css('top', _top);
        }
        else {
            //没有定义top则垂直居中.
            _top = ($(window).height() - $(_kwindow).outerHeight()) * 0.5; //outerHeight()
            _top += (this.config.topAdd ? this.config.topAdd : 0);
            _kwindow.css('top', _top);
            $(window).resize(function () {
                _top = ($(window).height() - $(_kwindow).outerHeight()) * 0.5; //outerHeight()
                _top += (_obj.config.topAdd ? _obj.config.topAdd : 0);
                _kwindow.css('top', _top);
            });
        }

    };
    //设置可拖动
    obj.prototype.move = function () {
        if (this.move == false) {//不允许拖动
            return;
        }
        var _kwindow = $('#kui-window-' + this.index + '');
        var _dragPart = $('#kui-window-' + this.index + ' ' + this.config.move + '');
        _dragPart.css('cursor', 'move');
        var _obj = this;
        $(_dragPart).mousedown(function (e) {
            _obj.dragState = { preX: e.pageX,preY:e.pageY };
        });
        $(document).mousemove(function (e) {
            if (!_obj.dragState) {
                return;
            }
            //$("#debugInfo").text(e.pageX + ", " + e.pageY);
            var cssLeft = $(_kwindow).css('left');
            var cssTop = $(_kwindow).css('top');
            var left = cssLeft.substr(0, cssLeft.length - 2);
            left = parseFloat(left);
            var top = cssTop.substr(0, cssTop.length - 2);
            top = parseFloat(top);
            $(_kwindow).css('left', left + (e.pageX - _obj.dragState.preX));
            $(_kwindow).css('top', top + (e.pageY - _obj.dragState.preY));
            _obj.dragState.preX = e.pageX;
            _obj.dragState.preY = e.pageY;
        });
        $(document).mouseup(function (e) {
            if (!_obj.dragState) {
                return;
            }
            _obj.dragState = undefined;
        });

    };
    kwindow.getFrameIndex = function (windowName) {
        return parseInt($("#" + windowName.replace('iframe-', '')).attr("kwindow-index"), 10);
    };
    kwindow.getArgs = function (index) {
        var obj = kwindow['obj_' + index];
        return obj.args;
    };
    kwindow.getIFrame = function (index, selector) {//kwindow.getChildFrame(selector, index) - 获取iframe页的DOM。例如：kwindow.getIFrame('body', index);
        //如果没有传index则从times属性中取出index.
        if (!index) {
            index = kwindow.index;
        }
        var _kwindow = $('#kui-window-' + index + '');
        if(!selector){
            return _kwindow.find('iframe').contents();//_kwindow.find('iframe').contents()[0]就是document对象.
        }
        else {
            return _kwindow.find('iframe').contents().find(selector);
        }
    };
    kwindow.iframeSrc = function (index, url, loadingConfig) {//kwindow.iframeSrc(index, url) - //重置特定iframe url
        if (!index) {
            index = kwindow.index;
        }
        var _kwindow = $('#kui-window-' + index + '');
        var loadingIndex = kwindow.loading(loadingConfig);
        _kwindow.find("iframe")[0].onload = function () {
            kwindow.close(loadingIndex);
        };
        _kwindow.find("iframe").attr("src", url);

    };
    kwindow.close = function (index) {
        if (index==undefined||index==null) {
            index = kwindow.index;
        }
        var _kwindow = $('#kui-window-' + index + '');
        if (_kwindow.length == 0) {//例如手动关闭后定时器又执行关闭.
            return;
        }
        var _obj = kwindow['obj_' + index];
        var _shade = $('#kui-window-shade-' + index + '');
        if (_obj.config.onClosing) {
            //var args={index:index,kwindow:_kwindow};
            _obj.config.onClosing(_obj.args);
            
        }
        if (_obj.args.close == false) {//不关闭
            _obj.args.close = true;//重置为默认值true.
            return;
        }

        _shade.remove();

        if (_obj.config.closeAnim != false) {
            var _timerID=window.setTimeout(function () {
                window.clearTimeout(_timerID);
                _kwindow.remove();
            }, _obj.config.closeAnim.speed)
            _kwindow.addClass('kui-anim-close');
        }
        else {
            _kwindow.remove();
        }

        if (_obj.config.onClosed) {
            _obj.config.onClosed(_obj.args);
        }
        kwindow['obj_' + index] = undefined;
        _obj = undefined;
    };
    kwindow.closeAll = function (type) {
        $.each($('.kui-window'), function (i, _kwindow) {
            var kwindowIndex = parseInt($(_kwindow).attr('kwindow-index'), 10);
            var kwindowType = parseInt($(_kwindow).attr('kwindow-type'), 10);
            if (!type || type == kwindowType) {
                kwindow.close(kwindowIndex);
            }
        });
    };
    kwindow.open = function (config) {
        var _obj = new obj(config);
        return _obj.index;
    };
    kwindow.msg = function (config) {
        
        var _config = $.extend({}, kwindow.config, { time: 3000, min: false, max: false, close: false, showTitleBar: false }, config, { type: kwindow.enums.type.msg });
       
        return kwindow.open(_config);
    };
    kwindow.alert = function (config) {
        var _config = $.extendEx({}, kwindow.config, { buttons: [{ text: '确定' }], min: false, max: false, close: true }, config, { type: kwindow.enums.type.alert });
        //alert(_config.buttons[0].style);
        //if ("function" == typeof config) {
        //    _config.button[0].onClick = config;
        //}
        //else {
        //    $.extendEx(_config, config);
        //}
        return kwindow.open(_config);
    };
    kwindow.confirm = function (config) {
        var _config = $.extendEx({}, kwindow.config, { buttons: [{ text: '确定' }, { text: '取消' }], min: false, max: false, close: true }, config, { type: kwindow.enums.type.confirm });

        //if ("function" == typeof config) {
        //    _config.button[0].onClick = config;
        //    _config.button[1].onClick = onOK;
        //}
        //else {
        //    $.extendEx(_config, config);
        //}
        return kwindow.open(_config);
    };
    kwindow.prompt = function (config) {
        //用户可以自定义content,取值时程序内部先取input元素的内容,若无input则取textarea元素的内容.
        var _config = $.extendEx({}, kwindow.config, { buttons: [{ text: '确定' }, { text: '取消' }], min: false, max: false, close: true }, config, { type: kwindow.enums.type.prompt });
        //if ("function" == typeof config) {
        //    _config.button[0].onClick = config;
        //    _config.button[1].onClick = onOK;
        //}
        //else {
        //    $.extendEx(_config, config);
        //}
        if (!_config.content) {
            if (!_config.promptType || _config.promptType == kwindow.enums.promptType.text) {
                _config.promptType = kwindow.enums.promptType.text;
                _config.content = '<input type="text" class="kui-window-input"/>';
            }
            else if (_config.promptType == kwindow.enums.promptType.textarea) {
                _config.content = '<textarea class="kui-window-input"></textarea>';
            }
        }
        return kwindow.open(_config);
    };
    kwindow.loading = function (config) {
        config = config || {};
        //config.IconCls //
        //loadingText:'加载中...',
        //loadingIconCls: '.loadingIconCls-1'

        var _config = $.extendEx({}, kwindow.config, { shade: { opacity: 0.01 }, content: '<div class="' + (config.loadingCls || "kui-window-loading-1") + '">' + (config.loadingText ? ("<div class=\"kui-window-loading-text\">" + config.loadingText + "</div>") : "") + '</div>', showTitleBar: false, min: false, max: false, close: false }, config, { type: kwindow.enums.type.loading });
        return kwindow.open(_config);
    };
    kwindow.win = function (config) {
        //alert('a');
        //alert(config.contentPartStyle);
        var _config = $.extendEx({}, kwindow.config, { sizeStatus: 'normal', max: {}, min: true, close: true,title:'窗口' }, config, { type: kwindow.enums.type.win });
        //alert(_config.contentPartStyle);
        return kwindow.open(_config);
    };
    
    kwindow.full = function (index) {
        if (!index) {
            index = kwindow.index;
        }
        var _kwindow = $('#kui-window-' + index + '');
        if (_kwindow.length == 0) {//例如手动关闭后定时器又执行关闭.
            return;
        }
        //var obj = kwindow['obj_' + index];
        var maxButton = $('#kui-window-' + index + ' .' + kwindow.enums.classes.max + '');//右上角最大化图标
        maxButton.click();
    };
    window.kwindow = kwindow;
    $k.alert = kwindow.alert;
    $k.confirm = kwindow.confirm;
    $k.prompt = kwindow.prompt;
    $k.loading = kwindow.loading;
    $k.win = kwindow.win;
}();